package com.demo.tools;

import java.math.BigDecimal;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;

import com.demo.exception.ExceptionMessage;
import com.demo.otherbeans.SystemInfoBean;

public class SystemInfo {
	Logger logger = Logger.getLogger(SystemInfo.class);
	
	public SystemInfoBean getSystemInfo(){
		logger.debug("===========>SystemInfo.getSystemInfo beging....");
		SystemInfoBean systemBean = null; 
		try{ 
			  Runtime r = Runtime.getRuntime();
			  Sigar sigar = new Sigar();
			  //如果报错时，将dll复制到以下输出的path下
			  //System.out.println(System.getProperty("java.library.path"));//获取java.library.path信息 
			  Mem mem = sigar.getMem(); 
			  CpuPerc cpuCerc = sigar.getCpuPerc();
			  systemBean = new SystemInfoBean();
			  
			  Properties props = System.getProperties(); 
			  MathExtend mathExtend = new MathExtend();
			  /* 
			  System.out.println("*****当前CPU使用情况 ：");                              
			  System.out.println("#总使用率: " + cpuCerc.getCombined() * 100 + "%");      
			  System.out.println("#用户使用率(user): " + cpuCerc.getUser() * 100 + "%");  
			  System.out.println("#系统使用率(sys): " + cpuCerc.getSys() * 100 + "%");    
			  System.out.println("#当前空闲率(idel): " + cpuCerc.getIdle() * 100 + "%");  
			  System.out.println("\n*****当前内存使用情况 ：");                           
			  System.out.println("#内存总量：" + mem.getTotal() / 1024 / 1024 + "M");     
			  System.out.println("#已使用内存：" + mem.getUsed() / 1024 / 1024 + "M");    
			  System.out.println("#剩余内存：" + mem.getFree() / 1024 / 1024 + "M");      
			  */
			  systemBean.setCpuCombined(mathExtend.round(cpuCerc.getCombined() * 100,2) + "%"); 
			  systemBean.setCpuUser(mathExtend.round(cpuCerc.getUser() * 100,2) + "%"); 
			  systemBean.setCpuSys(mathExtend.round(cpuCerc.getSys() * 100,2) + "%"); 
			  systemBean.setCpuIdle(mathExtend.round(cpuCerc.getIdle() * 100,2) + "%"); 
			  systemBean.setMemTotal(mem.getTotal() / 1024 / 1024 + "M"); 
			  systemBean.setMemUsed(mem.getUsed() / 1024 / 1024 + "M"); 
			  systemBean.setMemFree(mem.getFree() / 1024 / 1024 + "M"); 
			  double doublemenFreePercentage = Double.valueOf( mathExtend.divide(mem.getUsed(),mem.getTotal()));
			  doublemenFreePercentage = mathExtend.round(mathExtend.multiply(doublemenFreePercentage, 100),2);
			  systemBean.setMenFreePercentage(String.valueOf(doublemenFreePercentage));
			  /*
			  System.out.println("#剩余内存百分比：" + doublemenFreePercentage + "%");   
			  System.out.println("\n*****系统信息：");  
			  System.out.println("操作系统的名称：	" + props.getProperty("os.name"));    
			  System.out.println("操作系统的版本：	" + props.getProperty("os.version")); 
			  */
			  systemBean.setOsName(props.getProperty("os.name")); 
			  systemBean.setOsVersion(props.getProperty("os.version"));
		}catch (Exception e) {
			//logger.error(new ExceptionMessage().getExceptionDetail(e));
			e.printStackTrace();
		}
		logger.debug("===========>SystemInfo.getSystemInfo end....");
		return systemBean;
	}
	
	
	public static void main(String[] args) {
		SystemInfo info = new SystemInfo();
		info.getSystemInfo();
	}
	
	
	
 

	
}
